layui.use(['table', 'ax','admin','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;

    /**
    * 基础字典管理
    */
    var Level = {
        tableId: "levelTable"
    };

    /**
    * 初始化表格的列
    */
    Level.initColumn = function () {
        return [[
            {type: 'checkbox',hide:true},
            {field: 'levelId', hide: true, title: ''},
            {field: 'levelName', sort: false, title: '阶段名称'},
            {field: 'year', sort: false, title: '年度'},
            {field: 'sort', sort: false, title: '排序'},
            {field: 'updateTime', sort: false, title: '操作时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作',fixed:'right',minWidth:'250'}
        ]];
    };



    /**
    * 渲染表格
    */
    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Level.tableId,
        url: Feng.ctxPath + '/level/list',
        page: true,
        toolbar: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: Level.initColumn()
    });
    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Level.openAdd();
    });
    //搜索
    $('#btnSearch').click(function () {
        Level.search()
    })
    $("#btnExport").click(function () {
        Level.export()
    })

    $("#btnCopy").click(function () {
        Level.copy()
    })

    $("#btnExcelImport").click(function () {
        Level.openBatchAdd();
    })

    //批量添加
    Level.openBatchAdd = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '批量导入',
            area:['700px','400px'],
            content: Feng.ctxPath + '/level/toExcelImport',
            end: function () {
                //传给上个页面，刷新table用
                admin.putTempData('formOk', true);
                admin.getTempData('formOk') && table.reload(Level.tableId);
            }
        });
    }

    //添加
    Level.openAdd = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加',
            area:['300px','350px'],
            content: Feng.ctxPath + '/level/level_add',
            end: function () {
                admin.getTempData('formOk') && table.reload(Level.tableId);
            }
        });
    }
    //修改
    Level.onEdit = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '编辑',
            area:['300px','350px'],
            content: Feng.ctxPath + '/level/level_edit?levelId=' + data.levelId,
            end: function () {
                admin.getTempData('formOk') && table.reload(Level.tableId);
            }
        });
    }
    //查询
    Level.search = function () {
        var queryData = {};
        queryData['levelName'] = $("#levelName").val();
        queryData['year'] = $("#year").val();
        table.reload(Level.tableId, {where: queryData});
    }

    //删除
    Level.onDelete = function (data) {
        var operation = function () {
        var ajax = new $ax(Feng.ctxPath + "/level/delete", function () {
            Feng.success("删除成功!");
            Level.search()
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("levelId", data.levelId);
        ajax.start();
        };
        Feng.confirm('是否删除阶段"' + data.levelName + '"?删除阶段后内容和资料一并删除', operation);
    }

    //市内容
    Level.cityContent = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '市任务',
            area:['1000px','700px'],
            content: Feng.ctxPath + '/content/cityContent?levelId=' + data.levelId,
            end: function () {
                admin.getTempData('formOk') && table.reload(Level.tableId);
            }
        });
    }

    //区县内容
    Level.countyContent = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '区县内容',
            area:['1000px','700px'],
            content: Feng.ctxPath + '/content/countyContent?levelId=' + data.levelId,
            end: function () {
                admin.getTempData('formOk') && table.reload(Level.tableId);
            }
        });
    }

    /**
     * 年度任务复制
     */
    Level.copy = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '区县内容',
            area:['400px','300px'],
            content: Feng.ctxPath + '/level/toCopy',
            end: function () {
                admin.getTempData('formOk') && table.reload(Level.tableId);
            }
        });
    }
    
    //导出
    Level.export = function () {
        var year = $("#year").val();
        if(year==null || year=='' || year==undefined){
            Feng.error("请选择年度")
            return;
        }
        window.location.href = Feng.ctxPath + "/level/export?year="+year
    }

    // 工具条点击事件
    table.on('tool(' + Level.tableId + ')', function (obj) {
        var data = obj.data;
            var layEvent = obj.event;
        if (layEvent === 'edit') {
            Level.onEdit(data);
        } else if (layEvent === 'delete') {
            Level.onDelete(data);
        }else if (layEvent === 'city'){
            Level.cityContent(data);
        } else if (layEvent === 'county'){
            Level.countyContent(data);
        }
    });

    $("#levelName").keydown(function (event) {
        if(event.keyCode==13){
            Level.search();
        }
    })

    $("#year").change(function () {
        Level.search();
    })

    form.on("select(year)",function (data) {
        Level.search();
    })

});
